﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Util;
namespace DAO
{
    /// <summary>
    /// Summary description for Entry
    /// </summary>
    public class Entry
    {
        DBConnection DBConnection;
        public Entry()
        {
            DBConnection = new DBConnection();
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// Get all DTO.Entry instance from DB.
        /// </summary>
        /// <returns>List<> of all DTO.Entry instances.</returns>
        public List<DTO.Entry> GetAllEntrys()
        {
            Log.LogInfo("Begin GetAllEntrys()");
            String query = "SELECT * FROM [Entry];";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            List<DTO.Entry> entryDTOs = new List<DTO.Entry>();
            try
            {
                while (reader.Read())
                {
                    entryDTOs.Add(new DTO.Entry(reader));
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetAllEntrys(): " + ex.StackTrace);
            }

            Log.LogInfo("End GetAllEntrys()");
            DBConnection.Close();
            return entryDTOs;
        }
        /// <summary>
        /// Get DTO.Entry instance from DB.
        /// </summary>
        /// <param name="ID">Entry ID</param>
        /// <returns>DTO.Entry instance which has the specific ID.</returns>
        public DTO.Entry GetEntry(String ID)
        {
            Log.LogInfo("Begin GetEntry(String ID)");
            String query = "SELECT * FROM [Entry] WHERE [Entry].[ID]='" + ID + "';";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            DTO.Entry entryDTO = null;
            try
            {
                if (reader.Read())
                    entryDTO = new DTO.Entry(reader);
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetEntry(String ID): " + ex.Message);
            }

            Log.LogInfo("End GetEntry(String ID)");
            DBConnection.Close();
            return entryDTO;
        }

        /// <summary>
        /// Insert Entry into database.
        /// </summary>
        /// <param name="Entry">DTO.Entry instance to insert into database.</param>
        public int InsertEntry(DTO.Entry Entry)
        {
            Log.LogInfo("Begin InsertEntry()");
            String query = "";
            query += " INSERT INTO ENTRY (UserID, CategoryID, Title, Content,Actived,DatePosted,ViewPolicy,CommentPolicy) VALUES ('" + Entry.UserID + "','" + Entry.CategoryID + "','" + Entry.Title + "','" + Entry.Content + "','" + Entry.Actived + "','" + Entry.DatePosted + "','" + Entry.ViewPolicy + "','" + Entry.CommnentPolicy + "')";
            query += "";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Insert " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertEntry()");
            return result;
        }

        /// <summary>
        /// Update Entry info into database.
        /// </summary>
        /// <param name="Entry">Entry instance to be updated.</param>
        public void UpdateEntry(DTO.Entry Entry)
        {
            Log.LogInfo("Begin UpdateEntry()");
            String query = "UPDATE ENTRY SET UserID = '" + Entry.UserID + "'," + " Category = '" + Entry.CategoryID + "'Title ='" + Entry.Title + "'Content = '" + Entry.Content + "' WHERE ID = '" + Entry.ID + "'";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Update " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertEntry()");
        }
        /// <summary>
        /// Delete a specific Entry from DB.
        /// </summary>
        /// <param name="Entry">The Entry to be deleted.</param>
        public void DeleteEntry(DTO.Entry Entry)
        {
            Log.LogInfo("Begin DeleteEntry()");
            String query = "DELETE FROM [Entry] WHERE [Entry].[ID]='" + Entry.ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteEntry()");
        }

        /// <summary>
        /// Delete a Entry in database using ID.
        /// </summary>
        /// <param name="ID">Entry ID.</param>
        public void DeleteEntry(String ID)
        {
            Log.LogInfo("Begin DeleteEntry()");
            String query = "DELETE FROM [Entry] WHERE [Entry].[ID]='" + ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteEntry()");
        }
    }
}